#ifndef DATA_LIST_H
#define DATA_LIST_H

#define MAX_RECORDS 100
#define MAX_CMD_LENGTH 256

// 传感器类型
typedef enum {
    TEMP_AND_HUM,
    CURTAIN,
    LIGHT,
} DeviceType;

typedef union 
{
    unsigned long data;
    unsigned char light;
    unsigned char curtain;
}GeneralData;

// 传感器数据结构
typedef struct {
    float temperature;
    float humidity;
    char  ipd[2];
    int sensorid;
    unsigned long timestamp;
    int sensorname;
    GeneralData gdata;
    DeviceType device_type;
} SensorData;

// 数据链表节点结构
typedef struct DataNode {
    char ip_address[16];  // IPv4地址最长15字符+1结束符
    SensorData data;
    struct DataNode* next;
} DataNode;

// 命令链表节点结构
typedef struct CmdNode {
    char command[MAX_CMD_LENGTH];
    struct CmdNode* next;
} CmdNode;

// 数据链表管理结构
typedef struct {
    DataNode* head;
    int count;
} DataList;

// 命令链表管理结构
typedef struct {
    CmdNode* head;
    int count;
} CmdList;

/* ===== 数据链表操作 ===== */
void init_data_list(DataList* list);
int add_data_record(DataList* list, const char* ip, const SensorData* sensor_data);
int delete_data_record(DataList* list, const char* ip);
int update_data_record(DataList* list, const char* ip, const SensorData* new_data);
DataNode* find_data_record(DataList* list, const char* ip);
void print_all_data_records(const DataList* list);
void clear_data_list(DataList* list);

/* ===== 命令链表操作 ===== */
void init_cmd_list(CmdList* list);
int add_cmd_record(CmdList* list, const char* command);
int delete_cmd_record(CmdList* list, const char* command);
CmdNode* find_cmd_record(CmdList* list, const char* command);
void print_all_cmd_records(const CmdList* list);
void clear_cmd_list(CmdList* list);

#endif // DATA_LIST_H
